//
//  FQPickInfoView.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/23.
//

import UIKit
import AttributedString

class FQPickInfoView: UIView {
    let titleLb = QMUILabel(font: .mSFont(20), textColor: .color51)
    let detailV = UIView()
    let closeBtn = QMUIButton()
    var btnArray : [QMUIButton] = []
    
    var closeBlock : ((_ str : String)->Void)?
    var popup : KLCPopup!

    var sureStr = ""
    
    var model : FQFillInfoModel! {
        didSet {
            let str = model.title
            var str1 = "（单选）"
            closeBtn.setImage(.qmui_image(with: .navClose, size: CGSize(width: 12, height: 12), tintColor: .black), for: .normal)
            closeBtn.setTitle(nil, for: .normal)
            
            let astr : ASAttributedString = """
            \(str) \(str1, .foreground(.color102), .font(.rSFont(12)))
            """
            titleLb.attributed.text = astr
            
            detailV.qmui_removeAllSubviews()
            
            btnArray.removeAll()
            for str in model.detailArray {
                let button = QMUIButton(title: str, titleColor: .color102, font: .rSFont(16))
                button.layer.cornerRadius = 8
                button.clipsToBounds = true
                button.setTitleColor(.white, for: .selected)
                button.setBackgroundWithColor(.color238, for: .normal)
                button.setBackgroundWithColor(.colorMain, for: .selected)
                button.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
                detailV.addSubview(button)
                btnArray.append(button)
            }
            let height1 = ceil(Double(model.detailArray.count) / 2.0) * 51
            let height2 = (ceil(Double(model.detailArray.count) / 2.0) - 1) * 10
            detailV.snp.remakeConstraints { make in
                make.left.equalTo(20)
                make.right.equalTo(-20)
                make.top.equalTo(titleLb.snp.bottom).offset(24)
                make.height.equalTo(height1 + height2)
            }
            btnArray.snp.makeConstraints { make in
                make.height.equalTo(51)
            }
            btnArray.snp_distributeSudokuViews(fixedLineSpacing: 10, fixedInteritemSpacing: 10, warpCount: 2)
        }
    }
    
    convenience init() {
        self.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 323))
        clipsToBounds = true
        layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        layer.cornerRadius = 22
        backgroundColor = .white
        
        addSubview(titleLb)
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(24)
            make.height.equalTo(28)
        }
        
        closeBtn.titleLabel?.font = .mSFont(18)
        closeBtn.setTitleColor(.colorMain, for: .normal)
        closeBtn.addTarget(self, action: #selector(closeAction), for: .touchUpInside)
        addSubview(closeBtn)
        
        closeBtn.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.centerY.equalTo(titleLb)
        }
        
        addSubview(detailV)
        detailV.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(titleLb.snp.bottom).offset(24)
        }
    }

    @objc func buttonClick(_ button : QMUIButton) {
        for btn in btnArray {
            btn.isSelected = false
        }
        button.isSelected = true
        if closeBlock != nil {
            closeBlock!(button.titleLabel!.text!)
        }
        close()
    }
    
    @objc func closeAction() {
        close()
    }
    
    func show() {
        popup = KLCPopup(contentView: self, showType: .slideInFromBottom, dismissType: .slideOutToBottom, maskType: .dimmed, dismissOnBackgroundTouch: true, dismissOnContentTouch: false)
        popup.show(with: KLCPopupLayout(horizontal: .center, vertical: .bottom))
    }
    
    func close() {
        popup.dismiss(true)
    }
}
